package Classic150;

import java.util.ArrayList;
import java.util.List;

public class 汇总区间 {
    /*
        使用StringBuilder加快拼接字符串速度
     */
    public List<String> summaryRanges(int[] nums) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            int start = nums[i];
            while (i < nums.length - 1 && nums[i + 1] - nums[i] == 1) i++;
            int end = nums[i];
            StringBuilder sb = new StringBuilder();
            sb.append(start);
            if (start < end) {
                sb.append("->");
                sb.append(end);
            }
            list.add(sb.toString());
        }
        return list;
    }
}
